cmake_minimum_required(VERSION 3.5)

project(code_generator VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)

set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets REQUIRED)

#Grantlee5 for string template
find_package(Grantlee5 REQUIRED)

get_property(Grantlee_PLUGIN_DIR TARGET Grantlee5::defaulttags PROPERTY LOCATION)
get_filename_component(Grantlee_PLUGIN_DIR ${Grantlee_PLUGIN_DIR} PATH)
get_filename_component(Grantlee_PLUGIN_DIR ${Grantlee_PLUGIN_DIR} PATH)
get_filename_component(Grantlee_PLUGIN_DIR ${Grantlee_PLUGIN_DIR} PATH)

configure_file(grantlee_paths.h.cmake ${PROJECT_BINARY_DIR}/grantlee_paths.h)

include_directories( %{Qt:QT_INSTALL_PREFIX} )

qt5_add_resources(
  codegen_example_RCS_SRCS
  custom_tags.qrc
  OPTIONS -root "/plugins/grantlee/${Grantlee5_VERSION_MAJOR}.${Grantlee5_VERSION_MINOR}/"
)

qt5_add_resources(
  ui_style
  darkStyle/style.qrc
)


file(GLOB_RECURSE ANTLR_PARSER_GEN cpp-gen/*)
set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        grammar_parser.cpp
        ANTLRv4LexerBase.cpp
        ANTLRv4ParserBase.cpp
        grammar_parser.hpp
        mainwindow.h
        mainwindow.ui
        enginefactory.h
        enginefactory.cpp
        ${ANTLR_PARSER_GEN}
        ${codegen_example_RCS_SRCS}
        ${ui_style}
        codegen.qrc
)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(code_generator
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES}
        ${codegen_example_RCS_SRCS}
        ${ui_style}
        codegen.qrc
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET code_generator APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(code_generator SHARED
            ${PROJECT_SOURCES}
            ${codegen_example_RCS_SRCS}
            ${ui_style}
            codegen.qrc
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(code_generator
            ${PROJECT_SOURCES}
            ${codegen_example_RCS_SRCS}
            ${ui_style}
            codegen.qrc
        )
    endif()
endif()

target_link_libraries(code_generator 
PRIVATE  Qt${QT_VERSION_MAJOR}::Widgets
 antlr4_shared
 Grantlee5::Templates
)

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(code_generator)
endif()
